var img_file;
var ischange = false;
$(".input_file").change(function (e) {
    //console.log(e);
    var fileMsg = e.currentTarget.files;
    //console.log(fileMsg.length);
    if(fileMsg.length===0&&!ischange){
        $("#change").attr("disabled","disabled");
        return;
    }else {
        ischange = true;
    }
    if(ischange){
        $("#change").removeAttr("disabled");
    }
    if(fileMsg.length!==0){
        var objUrl = getObjectURL(this.files[0]);
        var size = this.files[0].size;
        if(size>=2*1024*1024){
            $(".alert-warning").css("display","block");
            return;
        }
        if(objUrl){
            $("#image").attr("src", objUrl);
            //console.log(this.files[0]);
            img_file = this.files[0];
        }
    }

});


$("#change").click(function () {

    var formData = new FormData();
    formData.append("file",img_file);

    $.ajax({
        type:"post",
        url:"/admin/upLoad",
        data:formData,
        processData:false,
        contentType:false,
        success:function (result) {
            //console.log(result);
            if(result==="error"){

            }else {
                $("#myModal").modal('hide');
                $("#headSculpture").attr('src',result);
                $("#image").attr('src',result);
                $("#img").attr('src',result);
            }
        }
    })
});

function getObjectURL(file)
{
    var url = null ;
    if (window.createObjectURL!==undefined)
    { // basic
        url = window.createObjectURL(file) ;
    }
    else if (window.URL!==undefined)
    {
        // mozilla(firefox)
        url = window.URL.createObjectURL(file) ;
    }
    else if (window.webkitURL!==undefined) {
        // webkit or chrome
        url = window.webkitURL.createObjectURL(file) ;
    }
    return url ;
}